﻿package 
{
	import flash.display.Shape;
	import flash.display.Sprite;	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.events.Event;
	
	/*
	 * coded by moka 2009 / 06/ 17 
	 * name : masklight
	 * extend : text effect
	 */

	public class Main extends Sprite 
	{		
		private var bmp:Bitmap;
				
		public function Main():void 
		{
			stage.scaleMode = "noScale"; // 設定場景不變形
			bmp = addChild(new Bitmap(new BitmapData(640, 480, true, 0xFF000000))) as Bitmap; // 場景呈現畫面			
			addEventListener(Event.ENTER_FRAME, onProcess); // loop 事件			
			(addChild(new TextField()) as TextField).htmlText = "<font color='#FFFFFF'>Moka's work 2009</font>"; // Moka's work 2009
		}		
		
		public function onProcess(e:Event):void {
			
			// 光源發射點，以 radial 方式填繪
			var light:Shape = new Shape();
			light.graphics.beginGradientFill("radial", [0xFFFFFF, 0], [1, 1],[0,70],new Matrix(1, 0, 0, 1, mouseX, mouseY));
			light.graphics.drawRect(0, 0, 640, 480);
			light.graphics.endFill();
			
			// 呈現文字
			var showTxt:TextField = new TextField();
			showTxt.htmlText = "<font color='#FFFFFF' size='45'>Moka's Text Glow Effect ! </font>";
			showTxt.width = 500;	
			
			var txtBmpBuffer:BitmapData = new BitmapData(640, 480, true, 0x00000000); // 用以點陣化文字
			var lightBmpBuffer:BitmapData = txtBmpBuffer.clone(); // 用以點陣化光源
			var show:BitmapData = txtBmpBuffer.clone(); // 最後效果疊加繪製的點陣圖暫存
			
			// 製作光源以文字作遮罩效果
			lightBmpBuffer.draw(light);
			txtBmpBuffer.draw(showTxt,new Matrix(1,0,0,1,70,200));			
			lightBmpBuffer.threshold(txtBmpBuffer, txtBmpBuffer.rect, new Point, "<", 0xFFFFFF); 
			
			// BlurFilter 與 add 效果疊加繪於 show
			for (var i:uint = 0; i < 5; i++ ){
				lightBmpBuffer.applyFilter(lightBmpBuffer, lightBmpBuffer.rect, new Point, new BlurFilter(10,10));
				show.draw(lightBmpBuffer, new Matrix, new ColorTransform(1- i * 0.09, 1- i * 0.05, 1, 1 - i * 0.09), "add");
			}

			// 清理畫面為黑色並繪製暫存圖於 show
			bmp.bitmapData.fillRect(bmp.bitmapData.rect, 0xFF000000);
			bmp.bitmapData.draw(show);
			bmp.bitmapData.draw(txtBmpBuffer);			
		}
	}	
}